/* 仅仅是对request方法，加一些请求的快捷方案 */
import { request } from '@umijs/max';
import _ from '@/assets/utils';

type RequestOptions = {
  method?: string;
  data?: any; // 根据需要调整类型
  [key: string]: any; // 允许其他属性
};

// 为request添加索引签名和调用签名
interface IRequest {
  (url: string, options?: RequestOptions): Promise<any>; // 添加调用签名
  [key: string]: (url: string, options?: RequestOptions) => Promise<any>;
}

// 确保request符合IRequest接口
const enhancedRequest: IRequest = request as unknown as IRequest;

['GET', 'HEAD', 'DELETE', 'OPTIONS'].forEach((name) => {
  enhancedRequest[name.toLowerCase()] = function (url: string, options?: RequestOptions) {
    options = options || {};
    if (!_.isPlainObject(options)) options = {};
    options.method = name;
    return enhancedRequest(url, options);
  };
});

['POST', 'PUT', 'PATCH'].forEach((name) => {
  enhancedRequest[name.toLowerCase()] = function (
    url: string,
    data: any,
    options?: RequestOptions,
  ) {
    options = options || {};
    if (!_.isPlainObject(options)) options = {};
    options.method = name;
    options.data = data;
    return enhancedRequest(url, options);
  };
});

export default enhancedRequest;
